package com.px.gateway.security;


import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
@EnableWebSecurity
public class ClientSecurityConfig {

    private static final String USERNAME = "admin";
    private static final String PASSWORD = "admin123";
    private static final String ROLE = "ADMIN";

    @Configuration
    class PreSecurityConfig extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    .requestMatchers()
                    .antMatchers("/login", "/logout")
                    .antMatchers("/swagger-ui.html", "/webjars/**", "/v2/api-docs", "/swagger-resources/**")
                    .and()
                    .authorizeRequests()
                    .antMatchers("/swagger-ui.html", "/webjars/**", "/v2/api-docs", "/swagger-resources/**")
                    .authenticated()
                    .and().formLogin()
                    .and().logout();
        }

        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            auth.userDetailsService(username -> new User(
                    USERNAME,
                    bCryptPasswordEncoder.encode(PASSWORD),
                    AuthorityUtils.commaSeparatedStringToAuthorityList(ROLE))).passwordEncoder(bCryptPasswordEncoder);
//            auth.inMemoryAuthentication().withUser(USERNAME).password(PASSWORD).roles(ROLE);
        }

    }
}
